//
//  ViewController.m
//  UIScrollViewDemo
//
//  Created by lim on 2019/6/25.
//  Copyright © 2019年 lim. All rights reserved.
//
#define kScreenWidth   [UIScreen mainScreen].bounds.size.width
#define kScreenHeight  [UIScreen mainScreen].bounds.size.height
#define SCROLLVIEW_HEIGHT _scrollView.bounds.size.height //testScrollview高度

#import "ViewController.h"

@interface ViewController ()<UIScrollViewDelegate>
{
    UIImageView *_leftImageView;
    UIImageView *_centerImageView;
    UIImageView *_rightImageView;
    NSInteger _currentImageIndex;
}
@property (nonatomic, strong) UIImageView *zoomImageView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSMutableArray *imageData;

@end

@implementation ViewController


/**
 *
 *  加载数据
 *
 */
- (NSMutableArray *)imageData{
    if (!_imageData) {
        _imageData = [NSMutableArray array];
        for (int i = 1;  i < 4; i++) {
            [_imageData addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i]]];
        }
    }
    return _imageData;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor grayColor];
    /*
     
     状态属性 描述
    tracking
     当用户手指与设备屏幕接触的时候为YES
     
     dragging
     当用户手指与设备屏幕接触并产生移动时为YES
     
     decelerating
     flick手势之后或拖动超过滚动区域并回弹后产生减速时为YES
     
     zooming
     捏放手势使zoomScale改变的时候为YES
     
     contentOffset
     scrollView的bounds的左上角位置
   
     
     
     这样讲可能有点抽象了，打个比方，将一张画布放在一个橱窗上展示，那么：
     
     scorllView <————> 橱窗
     
     contentView <————> 画布
     
     contentSize <————> 画布大小
     
     contentInset <————> 决定橱窗上的展示画布的区域
     
     contentOffset <————> 橱窗原点相对于画布当前位置的偏移
     */
    _scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    _scrollView.delegate = self;
    //设置最大伸缩比例
    _scrollView.maximumZoomScale = 2.0;
    //设置最小伸缩比例
    _scrollView.minimumZoomScale = 0.5;

    _scrollView.backgroundColor = [UIColor greenColor];
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    imgView.image = [UIImage imageNamed:@"www.jpeg"];
    [_scrollView addSubview:imgView];
    _zoomImageView = imgView;
    
    [self.view addSubview:_scrollView];
    
    _scrollView.contentSize = CGSizeMake(kScreenWidth * 3, 0);//内容大小
    
    _scrollView.showsHorizontalScrollIndicator = NO;//不显示水平拖地的条
    _scrollView.showsVerticalScrollIndicator = NO;//不显示垂直拖动的条
    _scrollView.pagingEnabled = YES;//不允许分页滑动
    _scrollView.bounces = NO;//到边了就不能再拖地
    
    //UIScrollView被push之后返回，会发生控件位置偏移，用下面的代码就OK
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [_scrollView setContentOffset:CGPointMake(kScreenWidth, 0)];
    
    [self addImageViews];
    [self loadDefaultImageView];
}



/**
 *
 *  添加图片
 *
 */
-(void)addImageViews{
    _leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, SCROLLVIEW_HEIGHT)];
    _leftImageView.contentMode = UIViewContentModeScaleAspectFit;
    [_scrollView addSubview:_leftImageView];
    
    _centerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth, 0, kScreenWidth, SCROLLVIEW_HEIGHT)];
    _centerImageView.contentMode=UIViewContentModeScaleAspectFit;
    [_scrollView addSubview:_centerImageView];
    
    _rightImageView=[[UIImageView alloc] initWithFrame:CGRectMake(2 * kScreenWidth, 0, kScreenWidth, SCROLLVIEW_HEIGHT)];
    _rightImageView.contentMode=UIViewContentModeScaleAspectFit;
    [_scrollView addSubview:_rightImageView];
}

/**
 *
 *  加载默认图片
 *
 */
-(void)loadDefaultImageView{
    _leftImageView.image = self.imageData.lastObject;//3
    _centerImageView.image = self.imageData.firstObject;//1
    _rightImageView.image = self.imageData[1];//2
    _currentImageIndex = 0;
    
}


/**
 *
 *  更新图片
 *
 */
-(void)updataImage{
    NSInteger leftImageIndex, rightImageIndex;
    //距离左边便宜多少
    CGPoint offset = _scrollView.contentOffset;
    if (offset.x > kScreenWidth) { //向右滑动
        _currentImageIndex = (_currentImageIndex + 1) % self.imageData.count;
    } else if(offset.x < kScreenWidth){ //向左滑动
        _currentImageIndex = (_currentImageIndex + self.imageData.count - 1) % self.imageData.count;
    }
    
    _centerImageView.image = self.imageData[_currentImageIndex];
    
    //重新设置左右图片
    leftImageIndex = (_currentImageIndex + self.imageData.count - 1) % self.imageData.count;
    
    rightImageIndex = (_currentImageIndex + 1) % self.imageData.count;
    NSLog(@"leftImageIndex==%ld rightImageIndex==%ld _currentImageIndex==%ld", leftImageIndex, rightImageIndex, _currentImageIndex);
    _leftImageView.image = self.imageData[leftImageIndex];
    _rightImageView.image = self.imageData[rightImageIndex];
}


#pragma mark - 任何偏移都会调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView { // any offset changes
    //NSLog(@"offset x == %f ",scrollView.contentOffset.x);
}

#pragma mark - ----------------------  手指触摸拖动相关 ----------------------
//
//// called on start of dragging (may require some time and or distance to move)
//#pragma mark - 开始拖动时调用(可能需要一些时间和距离来移动)
//- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
//    NSLog(@"将要开始拖动");
//}
//// called on finger up if the user dragged. velocity is in points/millisecond. targetContentOffset may be changed to adjust where the scroll view comes to rest
//- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
//    NSLog(@"将要结束拖动");
//
//}
//// called on finger up if the user dragged. decelerate is true if it will continue moving afterwards
//- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
//    NSLog(@"已经结束拖动");
//    //重新加载图片
//    [self updataImage];
//    //移动到中间
//    [scrollView setContentOffset:CGPointMake(kScreenWidth, 0)];
//}
//
//#pragma mark - ----------------------  手指松开滑动相关 ----------------------
//
//- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {   // called on finger up as we are moving
//      NSLog(@"手指松开开始滑动");
//}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {      // called when scroll view grinds to a halt
    NSLog(@"滑动结束");
    //重新加载图片
    [self updataImage];
    //移动到中间
    [scrollView setContentOffset:CGPointMake(kScreenWidth, 0)];

}

//- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView { // called when setContentOffset/scrollRectVisible:animated: finishes. not called if not animating
//    NSLog(@"设置动画调用");
//}
//
//#pragma mark - ----------------------  缩放相关 ----------------------
//#pragma mark - 用户使用捏合手势调用
//- (nullable UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {     // return a view that will be scaled. if delegate returns nil, nothing happens
//    return _zoomImageView;
//}
//
//- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view { // called before the scroll view begins zooming its content
//    NSLog(@"将要开始缩放");
//}
//
//#pragma mark - 正在缩放的时候调用
//- (void)scrollViewDidZoom:(UIScrollView *)scrollView { // any zoom scale changes
//    NSLog(@"正在缩放的时候调用");
//}
//
//#pragma mark - 缩放完毕的时候调用
//- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view atScale:(CGFloat)scale { // scale between minimum and maximum. called after any 'bounce' animations
//     NSLog(@"缩放结束");
//}
//
//#pragma mark - 如果你想滚动到顶部，返回一个yes。如果没有定义，假设是
//- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView {   // return a yes if you want to scroll to the top. if not defined, assumes YES
//    return YES;
//}
//
//- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView {       // called when scrolling animation finished. may be called immediately if already at top
//     NSLog(@"滚到顶调用");
//}
//
///* Also see -[UIScrollView adjustedContentInsetDidChange]
// */
//- (void)scrollViewDidChangeAdjustedContentInset:(UIScrollView *)scrollView {
//    NSLog(@"scrollView改变了ContentInset");
//}


@end
